/* Autostop
 *
 * Copyright (c) Maciej Gajewski, Grzegorz Latarowski
 * http://code.google.com/p/autostop-automaty-drogowe
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
 */



// gnet
#include "gnetserver.h"

// local
#include "serverthread.h"


// ===========================================================================
//                                ServerThread
// ===========================================================================



// ---------------------- ServerThread::ServerThread() ----------------------
ServerThread::ServerThread( const quint8 serverType, int socket, QObject * pParent )
	: QThread( pParent ), _serverType( serverType ), _socket( socket )
{
	// nope
}
// ---------------------------------------------------------------------------


// ---------------------- ServerThread::~ServerThread() ---------------------
ServerThread::~ServerThread()
{
	// nope
}
// ---------------------------------------------------------------------------


// --------------------------- ServerThread::run() ---------------------------
void ServerThread::run()
{
	QTcpSocket socket;
	socket.setSocketDescriptor( _socket );

	try
	{
		// create server
		GNetServer * pServer = 0;
		switch( _serverType )
		{
			case SERVER_ADJUSTER:
			{
					// say something
				qDebug( "AdjusterServer :: incoming connection from %s.",
						qPrintable( socket.peerAddress().toString() )
					  );
				pServer = new AdjusterServer( this );
			}
			break;

			case SERVER_BASE:
			{
					// say something
				qDebug( "BaseServer :: incoming connection from %s.",
						qPrintable( socket.peerAddress().toString() )
					  );
				pServer = new BaseServer( this );
			}
			break;

			default:
				break;
		}

			// start server 
		if( pServer )
		{
			pServer->setSocket( & socket );
			pServer->processPackets();
		}
	}
	catch( const GException& e )
	{
		qDebug("EventBaseServer: connection error: %s", qPrintable( e.getMessage() ) );
	}
}
// ---------------------------------------------------------------------------



// eof
